package edu.tms.crud;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Map;



import javax.sql.DataSource;

import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;


public class JdbcUtil {
	
	private static DataSource ds = ConnectionPool.getInstance();
	
	public static long insertAuto(String sql, Object... args)throws SQLException{
		Connection con=ds.getConnection();
		PreparedStatement pst=con.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
		if(args!=null && args.length>0){
			for(int i=0;i<args.length;i++){
				pst.setObject(i+1, args[i]);
			}
		}
		pst.executeUpdate();
		ResultSet rs=pst.getGeneratedKeys();
		long r=0;
		if(rs.next())r=rs.getLong(1);
		con.close();
		return r;
	}
	
	/**
	 * 更新
	 * @param sql sql语句
	 * @param args ？插入内容
	 * @return
	 * @throws SQLException
	 */
	public static int update(String sql, Object...args) throws SQLException{
		QueryRunner qr = new QueryRunner(ds);
		return qr.update(sql,args);
	}
	
	public static Map<String, Object> map(String sql, Object... args) throws Exception{
		QueryRunner qr = new QueryRunner(ds);
		return qr.query(sql, new MapHandler(),args);
	}
	
	public static List<Map<String, Object>> mapList(String sql, Object... args) throws Exception{
		QueryRunner qr = new QueryRunner(ds);
		return qr.query(sql, new MapListHandler(),args);
	}
	
	/**
	 * 查询实体类List
	 * @param clazz 需要填充的实体类
	 * @param sql sql语句 
	 * @param args ？插入内容
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List query(Class<?> clazz, String sql, Object... args) throws Exception{
		QueryRunner qr = new QueryRunner(ds);
		return qr.query(sql, new BeanListHandler(clazz,new BasicRowProcessor(new GenerousBeanProcessor())),args);
	}
	
	/**
	 * 单一实体类填充
	 * @param clazz 需要填充的实体类
	 * @param sql sql语句 
	 * @param args ？插入内容
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Object uniqueBean(Class<?> clazz, String sql, Object... args) throws Exception{
		QueryRunner qr = new QueryRunner(ds);
		return qr.query(sql, new BeanHandler(clazz,new BasicRowProcessor(new GenerousBeanProcessor())),args);
	}
	
	/**
	 * 查询单一数据  注意：count、max、min、last_insert_id()等返回Long
	 * @param sql sql语句
	 * @param col 查询列数 一般为1
	 * @param args ？插入内容
	 * @return  注意：count、max、min、last_insert_id()等返回Long
	 * @throws Exception
	 */
	public static Object uniqueValue(String sql, Integer col, Object... args) throws Exception{
		QueryRunner qr = new QueryRunner(ds);
		return qr.query(sql, new ScalarHandler<>(col), args);
	}
	
}
